package robuin.ioio.config;

//import robuin.ioio.config.IOIOSeek.SlowAdapter;
// pins having pwm o/p (only 9 active): 3,4,5,6,7,10,11,12,13,14,  
//										27,28,29,30,31,32,34,35,36,
//										37,38,39,40,45,46,47,48


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import robuin.ioio.config.R;
import ioio.lib.api.PwmOutput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.api.exception.OutOfResourceException;
import ioio.lib.util.android.IOIOActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.TextView;
import ioio.lib.util.BaseIOIOLooper;
import ioio.lib.util.IOIOLooper;

	public class servoOutput extends IOIOActivity{
		private List<SeekBar> listSeekBar_ = new ArrayList<SeekBar>();
		// only 9 at a time
		private List<Integer> pins = new ArrayList<Integer>(
				Arrays.asList(3,4,5,6,7,10,11,12,13));
	    private TextView mStatus;

	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);

	        Intent intent = getIntent();
	        String message = intent.getStringExtra(IOIOConfig.EXTRA_MESSAGE);
	        
	        setContentView(R.layout.servooutput);

	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar1));
	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar2));
	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar3));
	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar4));
	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar5));
	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar6));
	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar7));
	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar8));
	        listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar9));
	        //listSeekBar_.add((SeekBar)findViewById(R.id.SeekBar10));
	        
	        mStatus = (TextView) findViewById(R.id.status1);
	        mStatus.setText(message);
	        enableUi(false);
	    }
	    
	    class IOIOThread extends BaseIOIOLooper {
	    	private List<PwmOutput> listPwmOutputs_ = new ArrayList<PwmOutput>();
			
	    	@Override
			public void setup() throws ConnectionLostException {
				try {
					for(int i = 0; i < pins.size(); i++)
						listPwmOutputs_.add(ioio_.openPwmOutput(pins.get(i), 100));
					
					enableUi(true);
				} catch (ConnectionLostException e) {
					enableUi(false);
					throw e;
				} catch(OutOfResourceException e){
					Log.d("PWM", "Out of resource:" + e.getMessage());
				}
			}
			
	    	@Override
			public void loop() throws ConnectionLostException {
				try {
					int minSize = listSeekBar_.size() < listPwmOutputs_.size() ? listSeekBar_.size() : listPwmOutputs_.size();
					
					for(int i = 0; i < minSize; i++)
						listPwmOutputs_.get(i).setPulseWidth(500 + listSeekBar_.get(i).getProgress() * 2);
					
					Thread.sleep(100);
				} catch (InterruptedException e) {
					ioio_.disconnect();
				} catch (ConnectionLostException e) {
					enableUi(false);
					throw e;
				}
			}
	    	
	    	@Override
	    	public void disconnected(){
	    		for(int i = 0; i < listPwmOutputs_.size(); i++)
	    			listPwmOutputs_.get(i).close();
	    	}
		}
	    
	    /**
		 * A method to create our IOIO thread.
		 */
		@Override
		protected IOIOLooper createIOIOLooper() {
			return new IOIOThread();
		}
	    
		private void enableUi(final boolean enable) {
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					for(int i = 0; i < listSeekBar_.size(); i++)
						listSeekBar_.get(i).setEnabled(enable);
				}
			});
		}
		
	}